<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/*用户阅读时长*/

class ResourceUserRead extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_user_read';

    /**
     * 添加用户阅读时间
     * @param read_duration 阅读时长  单位分钟
     */
    public function addUserReadBook($user_id, int $read_duration = 1)
    {
        if(empty($user_id)){
            return false;
        }
        $res = $this->where('user_id', $user_id)->first();
        if (empty($res)) {
            $res = $this;
        } else {
            $read_duration = $res->read_duration + $read_duration;
        }
        $res->user_id = $user_id;
        $res->read_duration = $read_duration; //阅读时长
        $res->save();
        return true;
    }
}
